package com.endbd.systemmanagement.controller.logs;

import com.endbd.feignapi.model.system.Journal;
import com.endbd.systemmanagement.model.LogCondition;
import com.endbd.systemmanagement.model.PageUsers;
import com.endbd.systemmanagement.model.ResultBody;
import com.endbd.systemmanagement.service.logs.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("/system")
@RestController
public class LogController {

  @Autowired
  private LogService logService;

  @PostMapping("/logs/getLogList/{page}/{size}")
  public ResultBody getLogList(@PathVariable Integer page, @PathVariable Integer size,@RequestBody LogCondition logCondition){
    ResultBody resultBody = new ResultBody();
    PageUsers<Journal> list=logService.getLogList(page,size,logCondition);
    if(list.getList().isEmpty()){
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(list);
    }
    return resultBody;
  }

//  @PostMapping("/logs/record")
//  public boolean recordLog(@RequestParam String logMessage){
//    Journal journal = new Journal();
//    String[] split = logMessage.split(";");
//    journal.setUId(Integer.valueOf(split[1]));
//    journal.setModuleId(Integer.valueOf(split[2]));
//    journal.setJContent(split[0]);
//    journal.setJDate(new Date());
//    return logService.recordLog(journal);
//  }

  @PostMapping("/logs/record")
  public boolean recordLog(@RequestParam Integer uId, @RequestParam Integer moduleId, @RequestParam String content){
    Journal journal = new Journal();
    journal.setUId(uId);
    journal.setModuleId(moduleId);
    journal.setJContent(content);
    journal.setJDate(new Date());
    return logService.recordLog(journal);
  }

}
